<?
namespace Site\Model
{
    class Realisation extends \Api\Model\DB2
    {
        /**
         * Zwraca realizacje
         */
        public function getRealisationList()
        {         
            $Q = $this->db->query("SELECT a.*, b.name as file FROM arch_image_group as a LEFT JOIN arch_thumb as b ON a.id=b.group_id WHERE a.type='R'");                        
            
            $return=array('realisation'=>array());
            if( false !== ( $res = $Q->fetchAll() ))
            {
                foreach($res as $k => $v)
                {
                    $Q = $this->db->prepare("SELECT COUNT(`id`) as num FROM arch_image WHERE group_id=:rid");
                    $Q->bindValue(':rid',$v['id'],\PDO::PARAM_INT);
                    $Q->execute();
                    $res[$k]['images']=0;
                    if(false !== ($r = $Q->fetch()))
                    {
                        $res[$k]['images']=$r['num'];
                    }
                    $res[$k]['hash']=md5($v['id']);
                }
                $return['realisation']=$res;
            }
            return $return;
        }
        
        /**
         * Zwraca pojedyńczą realizację wraz 
         * z powiązanymi zdjęciami
         *
         * @param string $hash - hash realizacji md5(indeks + tytul)
         * @return array
         */
        public function getRealisationByHash($hash)
        {
            $Q = $this->db->prepare("SELECT * FROM `arch_image_group` WHERE 
                md5(`id`) = :hash
                LIMIT 1");
            $Q->bindValue('hash',$hash,\PDO::PARAM_STR);
            $Q->execute();
            
            if( false !== ( $res = $Q->fetch() ) )
            {
                $Q = $this->db->prepare("SELECT * FROM `arch_image` WHERE `group_id`=:rid");
                $Q->bindValue('rid',$res['id'],\PDO::PARAM_INT);
                $Q->execute();
                
                $res['images']=array();
                $res['c_images']=0;
                
                if( false !== ( $img = $Q->fetchAll() ) )
                {
                    $res['images']=$img;
                    $res['c_images']=count($img);
                }
                
                
                return $res;
            }
            
            return array();
        }
    }
}
?>